/*
Script: Services/Search.js

Copyright 2005-2007 Netvibes Ltd.
All rights reserved.
*/

if (typeof UWA == "undefined") var UWA = {};
if (typeof UWA.Services == "undefined") UWA.Services = {};
if (typeof UWA.Services.Search == "undefined") UWA.Services.Search = {};

UWA.Services.Search.getFromProvider = function(provider, request) {

   var options = "";

   if (typeof request.lang != 'undefined') {
       options += "&lang=" + request.lang;
   }
   if (typeof request.locale != 'undefined') {
       options += "&locale=" + request.locale;
   }
   if (typeof request.mode != 'undefined') {
       options += "&mode=" + request.mode;
   }

   switch (request.type) {
       case "websearch":
           var url = NV_PATH + 'data/websearch/' + '?q=' + encodeURIComponent(request.query) + '&engine=' + encodeURIComponent(provider) + options;
           return UWA.Data.request(url, { method : 'GET', type: 'json', onComplete: request.onComplete, context : request.context } );

       case "blogsearch":
           var url = NV_PATH + 'data/blogsearch/' + '?q=' + encodeURIComponent(request.query) + '&engine=' + encodeURIComponent(provider) + options;
           return UWA.Data.request(url, { method : 'GET', type: 'json', onComplete: request.onComplete, context : request.context } );

       case "imagesearch":
           var url = NV_PATH + 'data/imagesearch/' + '?q=' + encodeURIComponent(request.query) + '&engine=' + encodeURIComponent(provider) + options;
           return UWA.Data.request(url, { method : 'GET', type: 'json', onComplete: request.onComplete, context : request.context } );

       case "videosearch":
           var url = NV_PATH + 'data/videosearch/' + '?q=' + encodeURIComponent(request.query) + '&engine=' + encodeURIComponent(provider) + options;
           return UWA.Data.request(url, { method : 'GET', type: 'json', onComplete: request.onComplete, context : request.context } );

       case "podcastsearch":
           var url = NV_PATH + 'data/podcastsearch/' + '?q=' + encodeURIComponent(request.query) + '&engine=' + encodeURIComponent(provider) + options;
           return UWA.Data.request(url, { method : 'GET', type: 'json', onComplete: request.onComplete, context : request.context } );

       case "shoppingsearch":
           var url = NV_PATH + 'data/shoppingsearch/' + '?q=' + encodeURIComponent(request.query) + '&engine=' + encodeURIComponent(provider) + options;
           return UWA.Data.request(url, { method : 'GET', type: 'json', onComplete: request.onComplete, context : request.context } );

       case "opensearch":
           var url = NV_PATH + 'data/opensearch/' + '?q=' + encodeURIComponent(request.query) + '&url=' + encodeURIComponent(request.url) + options;
           return this.request(url, { method : 'GET', type: 'json', onComplete: request.onComplete, context : request.context } );

       default:
           UWA.log('invalid request type'); 
           break;
    }
}

// Retro-compatibility
UWA.Data.getFromProvider = UWA.Services.Search.getFromProvider